package club.hicode.daydayup.zookeeper.distribute0;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.*;
import org.junit.Test;
import org.springframework.test.context.TestPropertySource;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 测试
 *
 * @author Liu Chunfu
 * @date 2018-06-07 下午2:09
 **/
@Slf4j
public class MainTest {

    private static final String CONNECT_STR = "39.108.231.92:2181";

    @Test
    public void testDele() throws Exception {
        CountDownLatch startCdl = new CountDownLatch(1);
        ZooKeeper zooKeeper = new ZooKeeper(CONNECT_STR, 5 * 10 * 1000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    startCdl.countDown();
                }
            }
        });
        startCdl.await();

        String s = zooKeeper.create("/lock2", "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        log.info("创建{}成功", s);

        ThreadUtil.sleep(5 * 1000);
        zooKeeper.delete(s, -1);
        log.info("删除成功！");

    }


    @Test
    public void curatorTest() {
        CuratorFramework framework = CuratorFrameworkFactory.newClient(CONNECT_STR, new RetryNTimes(5, 1000 * 3));
        framework.start();

        InterProcessMutex mutex1 = new InterProcessMutex(framework, "/lock");
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 4; i++) {
            executorService.execute(() -> {
                try {
                    //System.out.println("thraed is " + Thread.currentThread().getId());
                    mutex1.acquire();
                    System.out.print("do...");
                    mutex1.release();
                    System.out.println("end...");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        ThreadUtil.sleep(5, TimeUnit.MINUTES);
    }

    @Test
    public void test1() {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        ZKLock zkLock = new ZKLock();
        for (int i = 0; i < 3; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    zkLock.lock();
                    //ThreadUtil.sleep(RandomUtil.randomInt(2), TimeUnit.SECONDS);
                    zkLock.unlock();
                }
            });
        }
        ThreadUtil.sleep(5, TimeUnit.MINUTES);
    }

    @Test
    public void test2() {
        List<String> list = CollectionUtil.newArrayList("lcf-001", "lcf-003", "lcf-002", "lcf-005", "lcf-007", "lcf-009");

        List<String> collect = list.stream().sorted().collect(Collectors.toList());
        System.out.println(collect);

        List<String> collect1 = list.stream().sorted().filter(v -> "lcf-003".compareTo(v) > 0).collect(Collectors.toList());
        System.out.println(collect1);


    }
}
